package com.itbaizhan.paymentdemo.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import jakarta.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.Map;

public class ZfbVerifierUtils {

    /**
     * 验证支付宝异步通知签名合法性
     * @param request   支付宝发送的请求
     * @param alipayPublickey  公钥
     * @return
     */
    public  static  boolean isVaild(HttpServletRequest request,String alipayPublickey){

        //获取支付宝post发送过来的信息
        //将数据存在map中
        Map<String,String> resultMap = new HashMap<>();
        //在request获取数据
        Map<String,String[]> requestParameterMap = request.getParameterMap();
        for(Object v: requestParameterMap.entrySet()){
            Map.Entry<String,String[]> item = (Map.Entry<String, String[]>)v; //强转
            resultMap.put(item.getKey(),item.getValue()[0]);
        }
        try {
            //true的返回
           return AlipaySignature.rsaCheckV1(resultMap,alipayPublickey,"UTF-8","RSA2");
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return false;
    }
}
